#include "PAP_defs.h"
#include "TPM_HAL_GB.h"
#include "ADC_HAL_GB.h"
#include "derivative.h"

#define GENERAL_TIME_BASE_TIMER 2
#define SCHEDULER_TIMER 1

#define SCHEDULER_LCD_TASK   			1
#define SCHEDULER_MENU_TASK    			2
#define SCHEDULER_SMAC_TASK     		3
#define SCHEDULER_SCI_TASK				4

#define RANDOM_NUMBER_GEN_CH    5
#define RANDOM_NUMBER_GEN_PIN   5

#define ADC_CALIBRATE_REQUEST   0x0F

#define GPIO_INPUT 0
#define GPIO_OUTPUT 1

#define MENU_REQUEST  0x01
#define LCD_REQUEST 0x10

#define STATUS_SHORT_CIRCUIT		0x80
#define STATUS_GPS_FIX				0x40
#define STATUS_MOTOR_OVERHEAT		0x20
#define STATUS_DRIVER_OVERHEAT		0x10
#define STATUS_ULTRASONIC_ENABLED	0x08
#define STATUS_HBLED_ON				0x04
#define STATUS_ACCELEROMETER_FAIL	0x02
#define STATUS_AIRPLANE_ON			0x01

#define CSTATUS_MOTOR_ON			0x80

#define DIR_MOVEMENT_N	0x80
#define DIR_MOVEMENT_S	0x40
#define DIR_MOVEMENT_E	0x10
#define DIR_MOVEMENT_W	0x20

#define DIR_POS_N	0x08
#define DIR_POS_S	0x04
#define DIR_POS_E	0x01
#define DIR_POS_W	0x02

#define BUTTON_GREEN       1<<0
#define BUTTON_RED         1<<1
#define BUTTON_BLUE        1<<2
#define BUTTON_YELLOW      1<<3

#define BUTTONS_PRESSED ( BUTTON_GREEN | BUTTON_YELLOW | BUTTON_RED | BUTTON_BLUE )

#define BUTTON_GREEN_HOLD  1<<4
#define BUTTON_RED_HOLD    1<<5
#define BUTTON_BLUE_HOLD   1<<6
#define BUTTON_YELLOW_HOLD 1<<7

#define BUTTONS_HOLD ( BUTTON_GREEN_HOLD | BUTTON_YELLOW_HOLD | BUTTON_RED_HOLD | BUTTON_BLUE_HOLD )

#define PLANE_CONF_FB_RESET_SURFACE_LIMITS   0x3F00

#define PLANE_CONF_FB_AILERON_MAX_LEFT	1<<13
#define PLANE_CONF_FB_AILERON_MAX_RIGHT	1<<12
#define PLANE_CONF_FB_RUDDER_MAX_LEFT	1<<11
#define PLANE_CONF_FB_RUDDER_MAX_RIGHT	1<<10
#define PLANE_CONF_FB_ELEVATOR_MAX_UP	1<<9
#define PLANE_CONF_FB_ELEVATOR_MAX_DOWN	1<<8

#define PLANE_CONF_FB_GPS	 		1<<7
#define PLANE_CONF_FB_AILERON_ZERO 	1<<6
#define PLANE_CONF_FB_RUDDER_ZERO	1<<5
#define PLANE_CONF_FB_ELEVATOR_ZERO	1<<4
#define PLANE_CONF_FB_ACCEL			1<<3
#define PLANE_CONF_FB_ULTRASONIC	1<<2
#define PLANE_CONF_FB_PLANE_ON		1<<1
#define PLANE_CONF_FB_MAIN_MOTOR	1<<0

#define AIPLANE_CONF_WAIT_TIMEOUT		4 //steps of 500 ms
#define SENSOR_MNGR_CONF_WAIT_TIMEOUT   4 //steps of 500 ms

#define AUTOMATIC_PILOT_ON			0x80

//Define AUTOPILOT_FREEFALL_ENABLED for enable automatic pilot turn on by free fall detected
//It will work only on air mode. If not defined, free fall will be ignored. 
#define AUTOPILOT_FREEFALL_ENABLED


void vfnServ_SetupTimeBase (void); 

uint8 u8Serv_RandomNumber (void);

void vfnServ_RandGenConfigure (void);

void vfnServ_SchedulerSetup (void);

void vfnMenu_service(void);

void vfnButtons_service(void);

void vfnMenuCounterService(void);
